<%@page import="com.jason.imagesnode.MyStringUtil"%>
<%@page import="org.json.JSONObject"%>
<%@page import="java.util.UUID"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>

<%@ page import="sun.misc.BASE64Decoder"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.io.File"%>

<%@page import="java.util.UUID"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.jspsmart.upload.SmartUpload"%>
<%@ page import="com.jspsmart.upload.Files"%>
<%@ page import="java.io.File"%>

<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="java.awt.image.RenderedImage"%>
<%@ page import="java.io.IOException"%>
<%@ page import="javax.imageio.ImageIO"%>
<%@ page import="org.icepdf.core.pobjects.Document"%>
<%@ page import="org.icepdf.core.util.GraphicsRenderingHints"%>

<%
	String sId = UUID.randomUUID().toString();
	String Sql = null;
	FileItemFactory factory = null;
	ServletFileUpload upload = null;
	List<FileItem> fileItems = null;
	Iterator<FileItem> iter = null;
	FileItem item = null;
	String ext = "txt";
	File file = null;
	String fileName = "";
	int number=1;

	JSONObject obj = new JSONObject();
	obj.put("result", "Y");
	boolean flag = true;
	try {
		request.setCharacterEncoding("UTF-8");
		factory = new DiskFileItemFactory();
		upload = new ServletFileUpload(factory);
		// 得到所有的表单域，它们目前都被当作FileItem
		fileItems = upload.parseRequest(request);
		iter = fileItems.iterator();
		//String basePath = Resources.getProperty("FileStore", Utils.getWebRootPath());
		//String filename = MyStringUtil.getFileName(UUID.randomUUID().toString().replace("-", ""));
		
		String postfixName = (String)request.getParameter("postfixName");// 服务器前缀
        if(MyStringUtil.isBlank(postfixName)){
                postfixName = "";
        }
		
		String basePath = request.getServletContext().getRealPath("/");
        String filename = UUID.randomUUID().toString().replace("-", "");
        filename = MyStringUtil.getFileName(filename,postfixName);
        String filepath = MyStringUtil.getFilePath(filename);
		
		
		//String filepath = Utils.getFilePath(filename);
		File f = new File(basePath + filepath);
		if (!f.exists()) {
			f.mkdirs();
		}
		
		String pdfout = "";
		
		String filenames = "";// 文件名
		int  i = 0;
		while (iter.hasNext()) {
			item = (FileItem) iter.next();
			if (!item.isFormField()) {
				fileName = item.getName();
				String[] strs = fileName.split("\\.");
				ext = strs[strs.length - 1];
				ext = ext.toLowerCase();
				file = new File(basePath + filepath+"/" + filename + "." + ext);
				pdfout = basePath + filepath+"/" + filename + "." + ext;
				
				if(i > 0){
					filenames += ",";
				}
				
				filenames += filename + "." + ext;
				
				item.write(file);
			} else {
				String x = item.getFieldName();
				String value = item.getString();
				if ("sId".equals(x)) {
					sId = value;
				}else if("number".equals(x)){
					try{
						number=Integer.parseInt(value);
					}catch(Exception e){}
				}
			}
		}
		//Utils.saveFile(filename, fileName, "-1", null, sId,number);
		
		System.out.println(basePath + filepath+"/" + filename + "." + ext);
		
		// 如果是pdf就进行切片
		if("pdf".equalsIgnoreCase(ext)){
			String pdfPath = pdfout;
			String path = basePath + filepath+"/";
			
			Document document = new Document();
	        document.setFile(pdfPath);
	        float scale = 1.5f;//缩放比例
	        float rotation = 0f;//旋转角度

	        for (int j = 0; j < document.getNumberOfPages(); j++) {
	            BufferedImage image = (BufferedImage)document.getPageImage(j, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
	            RenderedImage rendImage = image;
	            try {
	                String imgName = filename + "_" + j + ".png";
	                System.out.println(imgName);
	                File file1 = new File(path + imgName);
	                ImageIO.write(rendImage, "png", file1);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            image.flush();
	        }
	        document.dispose();
	        
	        filenames = filename + "_" + document.getNumberOfPages() + "." + ext;// 文件名
		}
		
		obj.put("filename",filenames);
	} catch (Exception e) {
		obj.put("errMsg", e);
		obj.put("result", "N");
		e.printStackTrace();
	} finally {
		try {
			factory = null;
			upload = null;
			fileItems = null;
			iter = null;
			item = null;
		} catch (Exception e) {
		}
		out.println(obj.toString());
	}
%>
